package com.example.administrator.meishi.Dao;

import com.example.administrator.meishi.Constract;
import com.example.administrator.meishi.bean.Title;
import com.example.administrator.meishi.tools.OkHttpClientUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/21.
 */
public class DownJson  {

    private static List<Title> titles;

    public static List<Title> getinfo() {
        titles = new ArrayList<>();
        String path = Constract.BASE_URL + Constract.CATEGOTY_URL;
        try {
            String json = OkHttpClientUtils.getStringFromUrl(path);
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                int id = object.getInt("id");
                String name = object.getString("name");
                Title title = new Title(id, name);
                titles.add(title);
            }
            return titles;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return  null;
    }
}
